/*
 * Copyright (C) 2013 The original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy
 * of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.zapta.apps.maniana.editors;

import java.util.Calendar;
import java.util.GregorianCalendar;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ToggleButton;

import com.zapta.apps.maniana.R;
import com.zapta.apps.maniana.annotations.MainActivityScope;
import com.zapta.apps.maniana.editors.EditRepeat;
import com.zapta.apps.maniana.model.ItemModel;

/**
 *
 * @author Elliott Mitchell
 */
@MainActivityScope
public class EditRepeatMonth extends EditRepeat
implements CompoundButton.OnCheckedChangeListener {

	@Override
	public View onCreateView(LayoutInflater inflator, ViewGroup container,
Bundle saved) {
		final View ret=super.onCreateView(inflator, container, saved);

		final ToggleButton subtype=(ToggleButton)ret.findViewById(
R.id.editor_repeat_by_type);
		subtype.setChecked(getArguments().
getBoolean("item_repeat_day_of_week", false));
		subtype.setOnCheckedChangeListener(this);

		final ToggleButton relend=(ToggleButton)ret.findViewById(
R.id.editor_repeat_relative);
		final boolean relend_state;
		if(!getArguments().containsKey("item_repeat_relative_end")) {
			final int dueDate=getArguments().getInt("item_due_date",0);
			final byte day=(byte)(dueDate&ItemModel.MASK_DAY);
			final byte maxday;
			final Calendar cal=new GregorianCalendar(
(dueDate>>>ItemModel.SHIFT_YEAR)&ItemModel.MASK_YEAR,
(dueDate>>>ItemModel.SHIFT_MONTH)&ItemModel.MASK_MONTH, day);
			maxday=(byte)cal.getActualMaximum(Calendar.DATE);

			relend_state=day-1>(maxday-1)>>>1;
			getArguments().putBoolean("item_repeat_relative_end",
relend_state);
		} else relend_state=getArguments().getBoolean(
"item_repeat_relative_end", false);
		relend.setChecked(relend_state);
		relend.setOnCheckedChangeListener(this);

		// clear state from other repeat types
		getArguments().remove("item_repeat_day_fixed");
		getArguments().remove("item_repeat_days");

		return ret;
	}

	@Override
	public void setArguments(Bundle bundle) {
		super.setArguments(bundle, "month");
	}

	@Override
	int getLayout() {
		return R.layout.editor_repeat_month;
	}

	@Override
	int getRepeatHeaderId() {
		return R.string.editor_repeat_month_header;
	}

	public void onCheckedChanged(CompoundButton but, boolean st) {
		final String key;
		switch(but.getId()) {
		case R.id.editor_repeat_by_type:
			key="item_repeat_day_of_week";
		break;
		case R.id.editor_repeat_relative:
			key="item_repeat_relative_end";
		break;
		default:
			return;
		}
		getArguments().putBoolean(key, st);
	}

	@Override
	int getRepeatPeriod() {
		return (getArguments().getByte("item_repeat_months", (byte)0)&0xFF)+1;
	}

	@Override
	int setRepeatPeriod(int count) {
		if(count>0x100) count=0x100; // max value
		getArguments().putByte("item_repeat_months", (byte)(count-1));
		return count;
	}
}

